/************************
 *Date: 14.07.2011
 *Year: 2011
 *Project: Thread_Exercises
 *File: Philosoph.java
 *************************
 *Description: 
 *************************
 *Author: Viktor
 *TODO
 *************************
 *******--History--*******
 *Version:
 *Changes:
 *Author:
 *************************/
package eating_philosoph;

/**
 * @author Viktor
 * 
 */
public class Philosoph extends Thread {

	private Gabel gabel_l, gabel_r;
	private String name;
	private Object key = new Object();

	public Philosoph(String name, Gabel g_l, Gabel g_r) {
		gabel_l = g_l;
		gabel_r = g_r;
		this.name = name;
	}

	public void run() {

		while (true) {
synchronized (key) {
	

			if ((gabel_l.isBesetzt() || gabel_r.isBesetzt())) {
				System.out.println("Philosoph " + name + " denkt.");

				try {
					Thread.sleep((int) (Math.random() * 100));
				} catch (InterruptedException ie) {

				}
			}else{
				gabel_l.setBesetzt(true);
				gabel_r.setBesetzt(true);
				System.out.println("Philosoph " + name + " isst.");
				try {
					Thread.sleep(((int) (Math.random() * 100)));
				} catch (InterruptedException ie) {

				}
				gabel_l.setBesetzt(false);
				gabel_r.setBesetzt(false);
			}
			}
		}

	}
}
